package com.xy.base.reciver;

import android.app.Activity;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

/**
 * Created by John on 2016/10/27.
 */

public class ExitReciver extends BroadcastReceiver {

    public static final String EXITACTION="app.exit";
    @Override
    public void onReceive(Context context, Intent intent) {
        if (context instanceof Activity){
            ((Activity) context).finish();
        }else if (context instanceof Service){
            ((Service)context).stopSelf();
        }
    }

    /**
     * 广播筛选
     **/
    public IntentFilter getIntentFilter() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ExitReciver.EXITACTION);
        return filter;
    }

    /**
     * 发送退出的广播
     */
    public static void exit(Context context){
        Intent intent = new Intent();
        intent.setAction(ExitReciver.EXITACTION);
        context.sendBroadcast(intent);
    }
}
